package com.example.basic01;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

@Slf4j
public class BoxDemo {

    @Test
    public void test01() {
        String str1 = "hello"; //str1指向静态区
        String str2 = new String("hello");  //str2指向堆上的对象
        String str3 = "hello";
        String str4 = new String("hello");
        System.out.println(str1.equals(str2)); //true
        System.out.println(str2.equals(str4)); //true
        System.out.println(str1 == str3); //true
        System.out.println(str1 == str2); //false
        System.out.println(str2 == str4); //false
        System.out.println(str2 == "hello"); //false
        str2 = str1;
        System.out.println(str2 == "hello"); //true
    }

    @Test
    public void test02() {
        Integer a = new Integer(3);
        Integer b = 3;  // 将3自动装箱成Integer类型
        int c = 3;
        System.out.println(a == b); // false 两个引用没有引用同一对象
        // 只要和基本类型比较都要先拆箱 所以比较的是值
        System.out.println(a == c); // true a自动拆箱成int类型再和c比较
        System.out.println(b == c); // true

        // [-128,127]
        Integer a1 = 128;
        Integer b1 = 128;
        System.out.println(a1 == b1); // false

        Integer a2 = 127;
        Integer b2 = 127;
        System.out.println(a2 == b2); // true
    }
}
